﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TWS.Controller.Paging;
using System.IO;
using TWS.Client;
using System.Data;

public partial class admin_QuanLyHTTT : BaseAdminPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            LoadHoTroTrucTuyen();

            //tab them moi
            LoadData();

        }
    }

    private void LoadHoTroTrucTuyen()
    {
        string tmpPage = Request.QueryString["pg"];
        int currentPage = 1;
        if (tmpPage != null && tmpPage != "")
        {
            currentPage = Convert.ToInt32(tmpPage);
        }
        HoTroTrucTuyenDataSet spDs = new HoTroTrucTuyenHelper().LoadHoTroTrucTuyen(currentPage, PagingSize, "NickName", "ASC");

        repSanPham.DataSource = spDs.HoTroTrucTuyen;
        repSanPham.DataBind();

        TotalRecord = new HoTroTrucTuyenHelper().LoadHoTroTrucTuyen().HoTroTrucTuyen.Rows.Count;

        ResultCount.Text = TotalRecord.ToString();

        Paging page = getPagingObject(TotalRecord, currentPage, PagingSize);

        PageString = page.BuildPaging();
    }

    private void LoadData()
    {
    }

    protected void ddlLoaiCha_OnSelectedIndexChanged(object sender, EventArgs e)
    {
    }

    protected void btnOK_OnClick(object sender, EventArgs e)
    {
        Page.Validate();
        if (!Page.IsValid) return;
        HoTroTrucTuyenHelper spHP = new HoTroTrucTuyenHelper();
        HoTroTrucTuyenDataSet spDS = new HoTroTrucTuyenDataSet();
        HoTroTrucTuyenDataSet.HoTroTrucTuyenRow HoTroTrucTuyenmoi = spDS.HoTroTrucTuyen.NewHoTroTrucTuyenRow();
        HoTroTrucTuyenmoi.NickName = txtTenSanPham.Text;
        HoTroTrucTuyenmoi.SDT = txtSDT.Text;
        spDS.HoTroTrucTuyen.AddHoTroTrucTuyenRow(HoTroTrucTuyenmoi);
        spHP.SaveHoTroTrucTuyen(spDS);
        RedirectPage(Request.RawUrl, true);
    }


    protected void learnMoreButton_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(((Button)sender).CommandArgument);
        HoTroTrucTuyenDataSet bannerDs = new HoTroTrucTuyenHelper().LoadHoTroTrucTuyen(id);
        bannerDs.HoTroTrucTuyen[0].Delete();
        new HoTroTrucTuyenHelper().SaveHoTroTrucTuyen(bannerDs);
        RedirectPage(Request.RawUrl, true);
    }

    protected void repSanPham_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Button repCon = ((Button)e.Item.FindControl("learnMoreButton"));
            repCon.Attributes.Add("onclick", "if (!confirm('Xóa dữ liệu?')) return;");
        }
    }
    public string GetNgayTao(object ngaytao)
    {
        if (ngaytao != DBNull.Value)
        {
            return CommonHelper.ConvertDate(ngaytao, Constants.AdminUI);

        }
        return "";
    }
}